﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Itcast.Chat.ViewModels;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Phone.UI.Input;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkID=390556 上有介绍

namespace Itcast.Chat.Views
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class LoginPage : Page
    {
        public LoginViewModel ViewModel { get; set; }
        public LoginPage()
        {
            ViewModel = new LoginViewModel();
            this.InitializeComponent();
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。
        /// 此参数通常用于配置页。</param>
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            //await StatusBar.GetForCurrentView().HideAsync();

            var statusBar = StatusBar.GetForCurrentView();

            statusBar.ProgressIndicator.ProgressValue = 0;
            statusBar.ProgressIndicator.Text = " ";

            // 在状态栏中展示进度条
            await statusBar.ProgressIndicator.ShowAsync();

            //var statusBar = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
            //await statusBar.ProgressIndicator.HideAsync();

            // 播放FaddIn动画
            fadeIn.Begin();


            HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        }

        #region 退出逻辑
        bool isExiting = false;
        async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            #region 异步实现
            //e.Handled = true;
            //// 短时间内（2秒钟）点两次后退键  则退出应用程序
            //if (isExiting)
            //{
            //    // 退出应用程序
            //    App.Current.Exit();
            //}
            //else
            //{
            //    // 展示正在退出提示 并将isExiting设为true
            //    exitTips.Visibility = Windows.UI.Xaml.Visibility.Visible;
            //    isExiting = true;
            //    //await Task.Run(() => { 

            //    //});
            //    await Task.Delay(2000);
            //    exitTips.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            //    isExiting = false;
            //} 
            #endregion

            if (isExiting)
            {
                App.Current.Exit();
            }
            else
            {
                exitTipShow.Begin();
                isExiting = true;
                exitTipShow.Completed += exitTipShow_Completed;
            }
            e.Handled = true;
        }

        void exitTipShow_Completed(object sender, object e)
        {
            // 动画播放完成 结束退出状态
            isExiting = false;
            exitTipShow.Completed -= exitTipShow_Completed;
        }

        //private void AppBarButton_Click(object sender, RoutedEventArgs e)
        //{
        //    exitTipShow.Begin();
        //} 
        #endregion
    }
}
